package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.UserInfoDTO;
import net.wanho.dto.UserPwdDTO;
import net.wanho.po.AuthInfo;
import net.wanho.po.UserInfo;
import net.wanho.service.UserInfoService;
import net.wanho.service.impl.UserInfoServiceImpl;
import net.wanho.vo.PageInfo;
import net.wanho.vo.R;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-01-10 17:14
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserInfoController {

    private final UserInfoService userInfoService;

    @GetMapping
    public List<UserInfo> list(){
        return userInfoService.list();
    }

    @GetMapping("/user-list")
    public R userList(@RequestParam(defaultValue = "1") int pageNum,
                      @RequestParam(defaultValue = "5") int pageSize,
                      UserInfoDTO userInfoDTO){
        PageInfo<UserInfo> pageInfo = userInfoService.listPage(pageNum, pageSize, userInfoDTO);
        return R.ok(pageInfo);
    }

    @GetMapping("/auth-list")
    public R authList(){
        List<AuthInfo> list = userInfoService.findMenu();
        return R.ok(list);
    }

    /**
     * 修改密码
     */
    @PutMapping("/modifyPwd")
    public R modifyPwd(@RequestBody UserPwdDTO userPwdDTO){
        userInfoService.modifyPwd(userPwdDTO);
        return R.ok("修改密码成功！");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable Integer id){
        UserInfo userInfo = userInfoService.getById(id);
        return R.ok(userInfo);
    }

    @PostMapping("/modifyAvatar/{id}")
    public R modifyAvatar(@PathVariable Integer id, @RequestParam("file")MultipartFile file){
        String url = userInfoService.modifyAvatar(id, file);
        return R.ok("上传成功", url);
    }
}
